home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 683 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: claus@faerber.muc.de (Claus A. Faerber)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: delete or delete[] ???
  5. Date: 10 Mar 1996 16:42:35 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <64ayxNJ3cDB@faerber.muc.de>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. Keywords: delete
  11. X-Original-Date: Sun, 10 Mar 1996 01:00:00 +0100
  12. In-Reply-To: <4hnqvf$mgr@fsgi01.fnal.gov>
  13. X-Mailer: CrossPoint v3.1 R/B17071
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMUN3GUy4NqrwXLNJAQEuPgIAid8cFpftvI32vNUnkHeKcwDt1TiaFBeT
  16.     VMyNQmFqm7juHMs96v75MaT8MsjCsZLECDgSbhCgd7lwV4MZtTEm9w==
  17.     =dDpH
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. David Sachs <b91926@fsgi01.fnal.gov> (07 Mar 96):
  21.  
  22. > Should delete or delete[] be used for a pointer to an array type?
  23. >
  24. > // It is assumed that some_type is an existing type
  25. > typedef some_type array_type[10];
  26. >
  27. > array_type* a1 = new array_type[5];
  28. > array_type* b1 = new some_type[5][10];
  29.  
  30. In both cases, you're creating a some_type[5][10], which is  
  31. an array_type[5] by typedef.
  32. It is an array of array_types, sou you have to use delete[].
  33.  
  34. > array_type* a2 = new array_type;
  35. > array_type* b2 = new some_type[10];
  36.  
  37. Here, you are creating a some_type[10] in both cases, which  
  38. is array_type.
  39. It is one single array_type, not an array of array_types, so  
  40. you have to use delete without [] in both cases.
  41.  
  42. > ...
  43.  
  44. > delete[] a1;
  45. > delete[] b1;
  46.  
  47. Ok, both point to arrays of the pointers' types.
  48.  
  49. > delete   a2; // or should it be delete[] a2;
  50.  
  51. Ok, points to a single array_type.
  52.  
  53. > delete[] b2; // or should it be delete   b2;
  54.  
  55. *No*, points also to a single array_type, so you actually  
  56. have to use delete b2.
  57.  
  58. Claus
  59.  
  60. ------------------------------------------------------------------------
  61. Claus Andre Faerber - claus@faerber.muc.de - http://www.muc.de/~cfaerber
  62. ------------------------------------------------------------------------
  63. ---
  64. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  65.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  66.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  67.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  68.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  69. ]
  70.